package com.CmJava.crowd.controller;


import com.CmJava.crowd.entity.ResultEntity;
import com.CmJava.crowd.entity.FundMoneyBy14Days;
import com.CmJava.crowd.entity.Good;
import com.CmJava.crowd.service.GoodsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.mail.MessagingException;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;


@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    private int pageNum=20;

    //获取商品列表
    @RequestMapping("goodList")
    @ResponseBody
    public PageInfo adminList(@RequestParam(value = "currentPage" ) String currentPage,
                              @RequestParam(value = "keyWord") String keyWord){
        int cp = Integer.parseInt(currentPage);
        PageInfo<Good> goodPageInfo=goodsService.goodsList(cp,pageNum,keyWord);
        return goodPageInfo;
    }


    // 获取单个商品的信息，从mysql取
    @RequestMapping("getOneGood")
    @ResponseBody
    public ResultEntity getOneGood(@RequestParam(value = "primaryKey" ) String primaryKey){
        ResultEntity resultEntity = goodsService.getOneGood(primaryKey);
        return resultEntity;
    }

    // 获取趋势图数据，来自redis
    @RequestMapping("getGoodChart")
    @ResponseBody
    public ResultEntity getGoodChart(@RequestParam(value = "primaryKey" ) String primaryKey)  {
        ResultEntity resultEntity = goodsService.getGoodChart(primaryKey);
        return resultEntity;
    }

    // 捐款
    @RequestMapping("fundGood")
    @ResponseBody
    public ResultEntity fundGood(@RequestParam(value = "primaryKey" ) String primaryKey,
                                 @RequestParam(value = "fundDate" ) String fundDate,
                                 @RequestParam(value = "fundMoney" ) String fundMoney)  {
        ResultEntity resultEntity = goodsService.updateGoodChart(primaryKey,fundDate,fundMoney);
        return resultEntity;
    }

    // 首页众筹信息展示
    @RequestMapping("indexGoodInf")
    @ResponseBody
    public ResultEntity indexGoodInf(HttpSession session)  {
        ResultEntity resultEntity = goodsService.indexGoodInf();
        return resultEntity;
    }


    // 首页今日获筹信息
    @RequestMapping("indexTodayInf")
    @ResponseBody
    public ResultEntity indexTodayInf(HttpSession session)  {
        ResultEntity resultEntity = goodsService.indexTodayInf();
        return resultEntity;
    }

    // 测试
     @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("testRedis")
    @ResponseBody
    public ResultEntity testredis() throws MessagingException {
        FundMoneyBy14Days fundMoneyBy14Days = new FundMoneyBy14Days(1, null);
        ArrayList<FundMoneyBy14Days.DateAndMoney> integers = new ArrayList<>();
        Random random = new Random();
        int i1;
        for (int i = 1; i <25 ; i++) {
            i1 = random.nextInt(60);
            HashMap<String, Integer> hashMap = new HashMap<>();
            hashMap.put("2020-12-"+i,i1);
            integers.add(fundMoneyBy14Days.new DateAndMoney(hashMap));
        }
        fundMoneyBy14Days.setFundMoney(integers);
        redisTemplate.opsForValue().set("1-14Days",fundMoneyBy14Days);
        System.out.println(fundMoneyBy14Days);
        return ResultEntity.successWithoutData();
    }



}
